use rand::TryRngCore;
use rbase::time_utils::get_systemtime_millis;
use sha256::digest;

pub fn validate_password(password: &String, hashed_password: &String, salt: &String) -> bool {
    let result = hash_password(password, salt);
    result == *hashed_password
}

pub fn hash_password(password: &String, salt: &String) -> String {
    digest(format!("{}{}", password, salt))
}

pub fn create_password(password: &String) -> (String, String) {
    let salt = digest(format!("{}{}{}", password, get_systemtime_millis(), rand::rngs::OsRng.try_next_u64().unwrap_or_default()));
    let hashed_password = hash_password(password, &salt);
    (hashed_password, salt)
}
